package com.tianbo.controller.json;

import com.tianbo.common.ModelAndPage;
import com.tianbo.controller.base.BaseController;
import com.tianbo.model.TKpiCargoManifest;
import com.tianbo.model.TKpiCargoManifestExample;
import com.tianbo.model.TKpiCargoManifestExample.Criteria;
import com.tianbo.service.ManiFestService;
import com.tianbo.util.dao.Page;
import com.tianbo.util.dao.Totals;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.math.BigDecimal;
import java.util.List;

@Controller
@RequestMapping("/man")
public class ManiFestJsonController extends BaseController{

    @Autowired
    ManiFestService maniFestService;


    @RequestMapping(value = "list.json",method = RequestMethod.POST)
    @RequiresRoles("admin")
    @ResponseBody
    public ModelAndPage<TKpiCargoManifest> maniList(@ModelAttribute TKpiCargoManifest manifest, @ModelAttribute Page frontPage){
        if(frontPage.getBegin()>=0){
            defaultDataStart = frontPage.getBegin();
        }
        if (frontPage.getLength()>0){
            defaultPerPage = frontPage.getLength();
        }

        TKpiCargoManifestExample example = new TKpiCargoManifestExample();
        Criteria criteria =  example.createCriteria();

        //根据时间段-年
        if(manifest.getCuryear()!=null&&manifest.getEndyear()!=null){
            criteria.andCuryearBetween(manifest.getCuryear(),manifest.getEndyear());
        }

        //根据时间段-月
        if(manifest.getCurmonth()!=null&&manifest.getEndmonth()!=null){
            criteria.andCurmonthBetween(manifest.getCurmonth(),manifest.getEndmonth());
        }

        //根据时间段-日
        if(manifest.getCurweek()!=null&&manifest.getEndweek()!=null){
            criteria.andCurweekBetween(manifest.getCurweek(),manifest.getEndweek());
        }

        //根据机号
        if(checkPropertyNull(manifest.getPlaneno())){
            criteria.andPlanenoEqualTo(manifest.getPlaneno());
        }
        //根据航班号
        if(checkPropertyNull(manifest.getFlightno())){
            criteria.andFlightnoEqualTo(manifest.getFlightno());
        }
        //根据机型
        if(checkPropertyNull(manifest.getPlanetype())){
            criteria.andPlanetypeEqualTo(manifest.getPlanetype());
        }
        //根据航空公司
        if(checkPropertyNull(manifest.getAircorpid())){
            criteria.andAircorpidEqualTo(manifest.getAircorpid());
        }
        //进出港
        if(checkPropertyNull(manifest.getExpimp())){
            criteria.andExpimpEqualTo(manifest.getExpimp());
        }
        //国内国际
        if(checkPropertyNull(manifest.getDomint())){
            criteria.andDomintEqualTo(manifest.getDomint());
        }
        //运单类型
        if(checkPropertyNull(manifest.getWaybilltype())){
            criteria.andWaybilltypeEqualTo(manifest.getWaybilltype());
        }
        //营业点
        if(checkPropertyNull(manifest.getOperdepartid())){
            criteria.andOperdepartidEqualTo(manifest.getOperdepartid());
        }

        int count = maniFestService.countByExample(example);
        BigDecimal weight = maniFestService.sumWeightByExample(example);
        BigDecimal pcs= maniFestService.sumPcsByExample(example);
        BigDecimal vol= maniFestService.sumVolByExample(example);
        Totals totals = new Totals(weight,pcs,vol);

        Page page = new Page(defaultDataStart,defaultPerPage);
        page.setCount(count);
        example.setPage(page); //oracle 用这个

        List<TKpiCargoManifest> maniList = maniFestService.selectByExample(example);
        ModelAndPage<TKpiCargoManifest> modelAndPage = new ModelAndPage<TKpiCargoManifest>(maniList,page,totals);
        return  modelAndPage;
    }
}
